package cn.com.jinhao.provider.business.controller;

import cn.com.jinhao.hotline.commons.controller.BaseController;
import cn.com.jinhao.hotline.commons.util.RestResponse;
import cn.com.jinhao.hotline.commons.vo.BaseRequestVO;
import cn.com.jinhao.hotline.commons.vo.business.RecordInfoRequestVO;
import cn.com.jinhao.hotline.commons.vo.business.record.RecordEventCondition;
import cn.com.jinhao.provider.business.entity.RecordInfo;
import cn.com.jinhao.provider.business.service.AddrService;
import cn.com.jinhao.provider.business.service.RecordFileService;
import cn.com.jinhao.provider.business.service.RecordInfoService;
import cn.com.jinhao.provider.business.service.TapeService;
import io.swagger.annotations.*;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * <p>Description: 记录事件服务</p>
 *
 * @author ShaneMew
 * @version 1.0
 * @date 2020/1/13
 * @since 2020-01-13
 */

@RestController
@RequestMapping("/record")
@Api(tags = {"记录事件服务"})
public class RecordInfoController extends BaseController {
    @Value("${system.area.province.code}")
    String provinceCode;
    @Value("${system.area.city.code}")
    String cityCode;
    @Resource
    RecordInfoService recordInfoService;
    @Resource
    TapeService tapeService;
    @Resource
    AddrService addrService;
    @Resource
    RecordFileService recordFileService;

    private final Log log = LogFactory.getLog(RecordInfoController.class);

    /**
     * 记录事件接口
     *
     * @param recordInfoRequestVO 事件信息请求类  {@link RecordInfoRequestVO}
     * @return resp       {@link RestResponse}
     */
    @ApiOperation(value = "记录事件", notes = "新增记录事件")
    @PostMapping
    public RestResponse<String> addEvent(@ApiParam(name = "recordInfo", value = "事件信息实体", required = true) @Validated({RecordInfoRequestVO.Create.class,BaseRequestVO.Create.class}) @RequestBody RecordInfoRequestVO recordInfoRequestVO, BindingResult result) throws Exception {
        validate(result);
        recordInfoRequestVO.setProvinceCode(provinceCode);
        recordInfoRequestVO.setProvince(addrService.queryNameByCode(provinceCode));
        recordInfoRequestVO.setCityCode(cityCode);
        recordInfoRequestVO.setCity(addrService.queryNameByCode(cityCode));
        recordInfoService.add(recordInfoRequestVO);
        log.debug("新增记录事件成功.....");
        return new RestResponse<>(null);
    }

    /**
     * 记录事件详情
     * @param  code 事件编码
     * @return resp {@link RestResponse}
     */
    @ApiOperation(value = "记录事件详情")
    @GetMapping
    public RestResponse<RecordInfo> detail(@ApiParam(name = "code", value="事件编码",required = true)@RequestParam String code) {
        RecordInfo recordInfo = recordInfoService.selectByCode(code);
        if (Optional.ofNullable(recordInfo).isPresent()) {
            recordInfo.setTapes(tapeService.queryTapsByEventCode(code));
        }
        return new RestResponse<>(recordInfo);
    }

    /**
     * 更新事件接口
     *
     * @param recordInfoRequestVO 事件信息请求类  {@link RecordInfoRequestVO}
     * @return resp       {@link RestResponse}
     */
    @ApiOperation(value = "更新事件", notes = "更新记录事件")
    @PutMapping
    public RestResponse<String> updateEvent(@ApiParam(name = "recordInfo", value = "事件信息实体", required = true) @Validated({RecordInfoRequestVO.Update.class, BaseRequestVO.Update.class}) @RequestBody RecordInfoRequestVO recordInfoRequestVO, BindingResult result) throws Exception {
        validate(result);
        recordInfoService.update(recordInfoRequestVO);
        return new RestResponse<>(null);
    }

    @ApiOperation(value = "事件查询", notes = "通过条件查询事件")
    @PostMapping(value = "/selectByCodnitions")
    public RestResponse<java.util.Map<String, Object>> selectByConditions(@ApiParam(name = "conditions", value = "事件查询条件") @Validated @RequestBody RecordEventCondition conditions, BindingResult result) throws Exception {
        validate(result);
        return new RestResponse<>(recordInfoService.selectByCondition(conditions));
    }

    @ApiOperation(value = "获取录音文件", notes = "根据CALLID获得录音文件")
    @GetMapping("/queryRecordFileByCallId")
    public RestResponse<String> queryRecordFileByCallId(@ApiParam(name = "callId", value = "callId") @RequestParam String callId) {
        return new RestResponse<>(recordFileService.queryRecordFileByCallId(callId));
    }
}
